package com.grapes.view;

import java.util.LinkedList;
import java.util.List;
import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.grapes.R;
import com.grapes.business.RecruiteInfoBusiness;
import com.grapes.model.NetApplication;
import com.grapes.widget.PullToRefreshListView;

public class NetApplicationListView extends PullToRefreshListView {
	private int selectedDailyInfoIndex = -1;
	private List<NetApplication> netApplications=new LinkedList<NetApplication>();
	private NetApplicationAdapter adapter;

	public NetApplicationListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public NetApplicationListView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public NetApplicationListView(Context context) {
		super(context);
		init();
	}
	
	private void init()
	{
		adapter=new NetApplicationAdapter();
		setAdapter(adapter);
	}
	
	public NetApplicationAdapter getListViewAdapter()
	{
		return adapter;
	}

	private void shareNetApplication() {

	}

	private void refuseNetApplication() {

	}

	private void favoriteNetApplication() {

	}

	//TODO
	public void reloadData()
	{
		new Thread(new Runnable() {
			@Override
			public void run() {
				netApplications = RecruiteInfoBusiness.getInstance()
						.getNetApplicationBefore(System.currentTimeMillis());
				Message msg = new Message();
				msg.what = RecruiteInfoListActivity.MSG_NET_APPLICATION_ALREADY;
				((RecruiteInfoListActivity)getController()).getControllerHandler().sendMessage(msg);
			}
		}).start();
	}
	private class CellViewHolder {
		TextView companyNameTxt;
		TextView timeThresholdTxt;
		TextView favoriteCountTxt;
		TextView commentCountTxt;
		Button shareBtn;
		Button refuseBtn;
		Button favoriteBtn;
		RelativeLayout upperGroup;
		RelativeLayout middleGroup;
		RelativeLayout bottomGroup;
	}

	class NetApplicationAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return netApplications.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			CellViewHolder viewHolder;
			TextView companyNameTxt;
			TextView timeThresholdTxt;
			TextView favoriteCountTxt;
			TextView commentCountTxt;
			Button shareBtn;
			Button refuseBtn;
			Button favoriteBtn;
			final RelativeLayout upperGroup;
			final RelativeLayout middleGroup;
			final RelativeLayout bottomGroup;

			if (convertView == null) {
				convertView = ((RecruiteInfoListActivity) getController())
						.getLayoutInflater().inflate(
								R.layout.net_application_item, null);
				viewHolder = new CellViewHolder();
				// TextView
				viewHolder.timeThresholdTxt = (TextView) convertView
						.findViewById(R.id.net_appication_time_threshold_txt);
				viewHolder.companyNameTxt = (TextView) convertView
						.findViewById(R.id.net_appication_company_name_txt);
				viewHolder.favoriteCountTxt = (TextView) convertView
						.findViewById(R.id.favorite_count_txt);
				viewHolder.commentCountTxt = (TextView) convertView
						.findViewById(R.id.comment_count_txt);
				// Button
				viewHolder.shareBtn = (Button) convertView
						.findViewById(R.id.share_btn);
				viewHolder.refuseBtn = (Button) convertView
						.findViewById(R.id.refuse_btn);
				viewHolder.favoriteBtn = (Button) convertView
						.findViewById(R.id.favorite_btn);
				// ViewGroup
				viewHolder.upperGroup = (RelativeLayout) convertView
						.findViewById(R.id.net_application_upper_group);
				viewHolder.middleGroup = (RelativeLayout) convertView
						.findViewById(R.id.net_application_btn_group);
				viewHolder.bottomGroup = (RelativeLayout) convertView
						.findViewById(R.id.net_application_bottom_group);
				convertView.setTag(viewHolder);
			} else
				viewHolder = (CellViewHolder) convertView.getTag();

			companyNameTxt = viewHolder.companyNameTxt;
			timeThresholdTxt = viewHolder.timeThresholdTxt;
			favoriteCountTxt = viewHolder.favoriteCountTxt;
			commentCountTxt = viewHolder.commentCountTxt;
			shareBtn = viewHolder.shareBtn;
			refuseBtn = viewHolder.refuseBtn;
			favoriteBtn = viewHolder.favoriteBtn;
			upperGroup = viewHolder.upperGroup;
			middleGroup = viewHolder.middleGroup;
			bottomGroup = viewHolder.bottomGroup;

			NetApplication netApplication = netApplications.get(position);
			companyNameTxt.setText(netApplication.getCompanyName());
			timeThresholdTxt.setText(netApplication.getStartDate() + " "
					+ netApplication.getStartTime() + "--"
					+ netApplication.getEndDate() + " "
					+ netApplication.getEndTime());
			favoriteCountTxt.setText(String.valueOf(netApplication
					.getFavoriteCount()));
			commentCountTxt.setText(String.valueOf(netApplication
					.getCommentCount()));
			if (position == selectedDailyInfoIndex) {
				middleGroup.setVisibility(View.VISIBLE);
			} else
				middleGroup.setVisibility(View.GONE);

			View.OnClickListener onRegionClickListener = new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					int height = middleGroup.getHeight() / 2;
					Animation.AnimationListener cellAnimationListener = new Animation.AnimationListener() {
						@Override
						public void onAnimationEnd(Animation animation) {
							adapter.notifyDataSetInvalidated();
						}

						@Override
						public void onAnimationRepeat(Animation animation) {

						}

						@Override
						public void onAnimationStart(Animation animation) {

						}
					};

					if (position == selectedDailyInfoIndex) {
						// to close
						TranslateAnimation downAnimation = new TranslateAnimation(
								0, 0, 0, height);
						downAnimation.setDuration(300);
						downAnimation
								.setAnimationListener(cellAnimationListener);
						upperGroup.startAnimation(downAnimation);

						TranslateAnimation upAnimation = new TranslateAnimation(
								0, 0, 0, -height);
						downAnimation.setDuration(300);
						downAnimation
								.setAnimationListener(cellAnimationListener);
						bottomGroup.startAnimation(upAnimation);
						selectedDailyInfoIndex = -1;
					} else {
						// to open up
						TranslateAnimation downAnimation = new TranslateAnimation(
								0, 0, 0, -height);
						downAnimation.setDuration(300);
						downAnimation
								.setAnimationListener(cellAnimationListener);
						upperGroup.startAnimation(downAnimation);

						TranslateAnimation upAnimation = new TranslateAnimation(
								0, 0, 0, height);
						downAnimation.setDuration(300);
						downAnimation
								.setAnimationListener(cellAnimationListener);
						bottomGroup.startAnimation(upAnimation);
						selectedDailyInfoIndex = position;
					}
				}
			};
			upperGroup.setOnClickListener(onRegionClickListener);
			bottomGroup.setOnClickListener(onRegionClickListener);

			shareBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					shareNetApplication();
				}
			});

			refuseBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					refuseNetApplication();
				}
			});

			favoriteBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					favoriteNetApplication();
				}
			});
			return convertView;
		}
	}

}
